<!DOCTYPE html>
<html lang="${LANG}" class="dark">

<head>
    <meta charset="UTF-8">
    <title>QBT_TR(Set location)QBT_TR[CONTEXT=HttpServer]</title>
    <link rel="stylesheet" href="css/style.css?v=${CACHEID}" type="text/css">
    <script defer src="scripts/localpreferences.js?v=${CACHEID}"></script>
    <script defer src="scripts/color-scheme.js?v=${CACHEID}"></script>
    <script defer src="scripts/pathAutofill.js?v=${CACHEID}"></script>
    <script>
        "use strict";

        window.addEventListener("DOMContentLoaded", (event) => {
            window.addEventListener("keydown", (event) => {
                switch (event.key) {
                    case "Enter":
                        event.preventDefault();
                        document.getElementById("setLocationButton").click();
                        break;
                    case "Escape":
                        event.preventDefault();
                        window.parent.qBittorrent.Client.closeFrameWindow(window);
                        break;
                }
            });

            const searchParams = new URLSearchParams(window.location.search);
            const path = searchParams.get("path");

            // set text field to current value
            if (path !== null)
                document.getElementById("setLocation").value = decodeURIComponent(path);

            document.getElementById("setLocation").focus();
            document.getElementById("setLocationButton").addEventListener("click", (e) => {
                e.preventDefault();
                e.stopPropagation();

                // check field
                const location = document.getElementById("setLocation").value.trim();
                if ((location === null) || (location === "")) {
                    document.getElementById("error_div").textContent = "QBT_TR(Save path is empty)QBT_TR[CONTEXT=TorrentsController]";
                    return;
                }

                fetch("api/v2/torrents/setLocation", {
                        method: "POST",
                        body: new URLSearchParams({
                            hashes: searchParams.get("hashes"),
                            location: location
                        })
                    })
                    .then(async (response) => {
                        if (!response.ok) {
                            document.getElementById("error_div").textContent = await response.text();
                            return;
                        }

                        window.parent.qBittorrent.Client.closeFrameWindow(window);
                    });
            });

            window.qBittorrent.pathAutofill.attachPathAutofill();
        });
    </script>
</head>

<body>
    <div style="padding: 10px 10px 0px 10px;">
        <label for="setLocation" style="font-weight: bold;">QBT_TR(Location:)QBT_TR[CONTEXT=TransferListWidget]</label>
        <input type="text" id="setLocation" class="pathDirectory" autocorrect="off" autocapitalize="none" style="width: 99%;">
        <div style="float: none; width: 99%;" id="error_div">&nbsp;</div>
        <div style="text-align: center; padding-top: 10px;">
            <input type="button" value="QBT_TR(Save)QBT_TR[CONTEXT=HttpServer]" id="setLocationButton">
        </div>
    </div>
</body>

</html>
